//
//  LNRouter.m
//  LNDevelopExample
//
//  Created by wwww on 2018/9/5.
//  Copyright © 2018年 淘发现1iOS. All rights reserved.
//

#import "LNRouter.h"

@interface LNRouter()

/// viewModel到viewController的映射
@property (nonatomic, copy) NSDictionary *viewModelViewMappings;

@end

@implementation LNRouter
static LNRouter *sharedInstance_ = nil;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [super allocWithZone:zone];
    });
    return sharedInstance_;
}

- (id)copyWithZone:(NSZone *)zone
{
    return sharedInstance_;
}

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [[self alloc] init];
    });
    return sharedInstance_;
}

- (LNViewController *)viewControllerForViewModel:(LNViewModel *)viewModel {
    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    
    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[LNViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);
    
    return [[NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}


/// 这里是viewModel -> ViewController的映射
/// If You Use Push 、 Present 、 ResetRootViewController ,You Must Config This Dict
- (NSDictionary *)viewModelViewMappings {
    return @{
             @"LNNewFeatureViewModel":@"LNNewFeatureViewController",
             @"LNMainViewModel":@"LNMainViewController",
             @"LNHomeViewModel":@"LNHomeViewController",
             @"LNNewsViewModel":@"LNNewsViewController",
             @"LNSerachViewModel":@"LNSearchViewController",
             @"LNProfileViewModel":@"LNProfileViewController",
             };
}
@end
